package com.hand.todo.api.controller.v1;

import com.hand.todo.app.service.SoLineService;
import com.hand.todo.config.SoLineTag;
import com.hand.todo.domain.entity.HodrSoHeader;
import com.hand.todo.domain.entity.HodrSoLine;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.hzero.core.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = SoLineTag.TAG)
@RestController("soLineController.v1")
@RequestMapping("/hptl/v1")
public class SoLineController {

    @Autowired
    private SoLineService lineService;

    @ApiOperation("订单行查询api")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping("/{organizationId}/so-lines/dto/{headerId}")
    public ResponseEntity<Page<HodrSoLine>> query(
            @PathVariable("organizationId") long orgId,
            @PathVariable("headerId") long headerId,
            PageRequest pageRequest
    ) {
        return Results.success(lineService.pageSelectBySoHeaderId(headerId, pageRequest));

    }

    @ApiOperation("订单行删除api")
    @Permission(level = ResourceLevel.SITE)
    @DeleteMapping("/{organizationId}/so-lines/dto")
    public void remove(
            @PathVariable("organizationId") long orgId,
            @RequestBody List<HodrSoLine> list,
            Long soHeaderId) {
        Long[] soLineIds = list.stream()
                .map(HodrSoLine::getSoLineId)
                .collect(Collectors.toList())
                .toArray(ArrayUtils.EMPTY_LONG_OBJECT_ARRAY);
        lineService.deleteBySoLineIds(Arrays.asList(soLineIds), soHeaderId);
    }


}
